package com.thssh.ui.lsn04.view;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/14
 * email: zhangyugehu@163.com
 */

public class WaveView extends View {
    private Paint mShaderPaint;
    private Paint mPaint;
    private RadialGradient mShader;

    int colors[] = new int[]{ Color.TRANSPARENT, Color.LTGRAY};
    int mWidth = 600;
    int mHeight = 200;
    float dRadius;
    int touchX;
    ValueAnimator shaderAnimator;

    public WaveView(Context context) {
        this(context, null);
    }

    public WaveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mShaderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.GRAY);
        shaderAnimator = ValueAnimator.ofInt(mHeight, mWidth);
        shaderAnimator.setDuration(500);
        shaderAnimator.setRepeatCount(0);
        shaderAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                dRadius = (int) animation.getAnimatedValue();
                invalidate();
            }
        });
        shaderAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                dRadius = 0;
                invalidate();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(shaderAnimator != null){
                    shaderAnimator.start();
                }
                touchX = (int) event.getX();
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(0, 0, mWidth, mHeight, mPaint);
        if(dRadius > 0) {
//            Log.d("WaveView", "touchX: " + touchY + " touchY: " + touchY);
            mShader = new RadialGradient(touchX, mHeight / 2, dRadius, colors, null, Shader.TileMode.CLAMP);
            mShaderPaint.setShader(mShader);
            canvas.drawCircle(touchX, mHeight / 2, dRadius, mShaderPaint);
        }
        mPaint.setColor(Color.WHITE);
        mPaint.setTextSize(60);
        mPaint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText("点击我！", mWidth / 2, mHeight / 2, mPaint);
    }
}
